package com.ihd.ihardware.view.scooter.view;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.utils.DataUtils;
import com.ihd.ihardware.databinding.ActivityScMeBinding;
import com.ihd.ihardware.pojo.CurrStatus;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.scooter.viewModel.SCViewModel;
import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.utils.BLEUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;

import static com.ihd.ihardware.common.AppConstans.Write_Character_UUID;
import static com.ihd.ihardware.common.AppConstans.Write_Service_UUID;

public class SCMeActivity extends BaseActivity<ActivityScMeBinding, SCViewModel> {
    private DeviceListRes.DataBean.ListBean mBD;
    private CurrStatus mCurrStatus = new CurrStatus();

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setLeftImageResource(R.drawable.bike_nb_back);
        binding.mtitlebar.setTitle("滑板车设置");
        binding.mtitlebar.setTitleColor(getResources().getColor(R.color.C_FFFFFF));

        mBD = (DeviceListRes.DataBean.ListBean) getIntent().getSerializableExtra("sc");
        LiveEventBus.get()
                .with("onNotify", String.class)
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                        Log.e("onNotify setting", s);
                        if (s.startsWith("5A0F03")) {
                            mCurrStatus = DataUtils.parseStatus(s);

                            binding.totalDistance.setText(String.valueOf(mCurrStatus.getTotalway()));
                            int ho = mCurrStatus.getRidetime() / 3600;
                            int mi = mCurrStatus.getRidetime() % 3600 / 60;
                            binding.totalH.setText(String.valueOf(ho));
                            binding.totalM.setText(String.valueOf(mi));

                            RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) binding.unit.getLayoutParams();
                            rl.removeRule(RelativeLayout.ALIGN_PARENT_START);
                            rl.removeRule(RelativeLayout.ALIGN_PARENT_END);
                            if (SPUtils.getBoolean("km", true)) {
                                rl.addRule(RelativeLayout.ALIGN_PARENT_START);
                                binding.unit.setText("公里");
                            } else {
                                rl.addRule(RelativeLayout.ALIGN_PARENT_END);
                                binding.unit.setText("英里");
                            }
                            binding.unit.setLayoutParams(rl);

                            RelativeLayout.LayoutParams assrl = (RelativeLayout.LayoutParams) binding.ass.getLayoutParams();
                            assrl.removeRule(RelativeLayout.ALIGN_PARENT_START);
                            assrl.removeRule(RelativeLayout.ALIGN_PARENT_END);
                            if (mCurrStatus.getMode() == 0) {
                                assrl.addRule(RelativeLayout.ALIGN_PARENT_START);
                                binding.ass.setBackgroundResource(R.drawable.circle_line_hw_20);
                            } else {
                                assrl.addRule(RelativeLayout.ALIGN_PARENT_END);
                                binding.ass.setBackgroundResource(R.drawable.circle_1acbff_006cff);
                            }
                            binding.ass.setLayoutParams(assrl);

                            RelativeLayout.LayoutParams cruiserl = (RelativeLayout.LayoutParams) binding.cruise.getLayoutParams();
                            cruiserl.removeRule(RelativeLayout.ALIGN_PARENT_START);
                            cruiserl.removeRule(RelativeLayout.ALIGN_PARENT_END);
                            if (mCurrStatus.getXh() == 0) {
                                cruiserl.addRule(RelativeLayout.ALIGN_PARENT_START);
                                binding.cruise.setBackgroundResource(R.drawable.circle_line_hw_20);
                            } else {
                                cruiserl.addRule(RelativeLayout.ALIGN_PARENT_END);
                                binding.cruise.setBackgroundResource(R.drawable.circle_1acbff_006cff);
                            }
                            binding.cruise.setLayoutParams(cruiserl);
                        }
                    }
                });

        binding.unitRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SPUtils.getBoolean("km", true)) {
                    SPUtils.putBoolean("km", false);
                } else {
                    SPUtils.putBoolean("km", true);
                }
            }
        });

        binding.assRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrStatus.getMode() == 1) {
                    BLEUtils.writePsw(mBD.getMac(), "A5050700", "0000", Write_Service_UUID, Write_Character_UUID);
                } else {
                    BLEUtils.writePsw(mBD.getMac(), "A5050701", "0000", Write_Service_UUID, Write_Character_UUID);
                }
            }
        });

        binding.cruiseRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrStatus.getXh() == 1) {
                    BLEUtils.writePsw(mBD.getMac(), "A5050800", "0000", Write_Service_UUID, Write_Character_UUID);
                } else {
                    BLEUtils.writePsw(mBD.getMac(), "A5050801", "0000", Write_Service_UUID, Write_Character_UUID);
                }
            }
        });

        binding.modifyPsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(SCMeActivity.this, SCPswActivity.class);
                mIntent.putExtra("sc", mBD);
                startActivity(mIntent);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sc_me;
    }

    @Override
    protected Class getViewModelClass() {
        return SCViewModel.class;
    }
}
